package com.xykj.enterprise.wechat.ext.api.schedule;

import com.xykj.enterprise.wechat.bean.ext.schedule.GetScheduleVo;
import com.xykj.enterprise.wechat.ext.service.schedule.ScheduleService;
import com.xykj.enterprise.wechat.util.string.StringUtil;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Function(description = "获取日程详情", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "schedule_id_list", type = Type.String, description = "日程ID列表。一次最多拉取1000条--逗号分隔", required = true),
})
@Component
public class GetSchedule implements Action {

    @Autowired
    private ScheduleService scheduleService;

    @Override
    public Response execute(Request request) throws Exception {

        String[] schedule_id_list = request.getString("schedule_id_list").split(",");

        List<String> list = new ArrayList<>();
        for (String scheduleId : schedule_id_list) {
            log.debug("日程ID遍历，scheduleId:{}", scheduleId);
            list.add(scheduleId);
        }

        HashMap<String, Object> params = new HashMap<>();
        params.put("schedule_id_list", list);

        GetScheduleVo rsp = scheduleService.getSchedule(
                request.getString("access_token"),
                params
        );
        if (rsp.getErrcode() != 0) {
            return Response.fail(rsp.getErrmsg());
        }
        return Response.success().put("data", rsp);
    }

}
